<?php
require_once '../sub/init.php';
if ( empty($ulist) )textmsg('forbidden');
require_once 'chkUadm.php';
if(!CRMQX('crm_user_guohu'))exit(noauth('暂无【客户归属修改】权限'));
$tmeplist = explode('_',$ulist);
if(@count($tmeplist)>=1){
	foreach($tmeplist as $uid){
		if ( !ifint($uid) )textmsg('uid：'.$uid.'不存在');
		$row = $db->ROW(__TBL_USER__,"areaid,agentid","id=".$uid,"name");
		if(!$row){alert_adm("UID输入有误或不存在此客户","back");}else{
			$areaid  = dataIO($row['areaid'],'out');
			$agentid_ = intval($row['agentid']);
			//门店+地区
			if(!CRM_ADM){
				if($agentid_!=$session_agentid)exit(noauth('暂无【跨门店操作】权限'));
				if(!empty($session_agentareaid) && str_len($session_agentareaid)>5){
					$areaidS = explode(',',$session_agentareaid);
					$m1=$areaidS[0];$m2=$areaidS[1];$m3=$areaidS[2];
					if (ifint($m1) && ifint($m2) && ifint($m3)){
						$areaidS = $m1.','.$m2.','.$m3;
					}elseif(ifint($m1) && ifint($m2)){
						$areaidS = $m1.','.$m2;
					}elseif(ifint($m1)){
						$areaidS = $m1;
					}
					if(!strstr($areaid,$areaidS))exit(noauth('暂无【跨地区操作】权限'));
				}
			}
			//门店+地区 结束
		}
	}
}
if ($submitok == 'sendupdate'){
	if ( !ifint($agentid) )textmsg('请选择要分配的客户【所属门店】','back','返回');
	if ( !ifint($admid) )textmsg('请选择要分配的客户【认领红娘】','back','返回');
	if($agentid){
		$row = $db->ROW(__TBL_CRM_AGENT__,"title","id=".$agentid,'name');
		$agenttitle = ($row)?$row['title']:'';
		$SQL = "agentid=".$agentid.",agenttitle='$agenttitle'";
	}else{
		textmsg('请选择要分配的客户【所属门店】','back','返回');
	}
	//
	if($admid){
		$row = $db->ROW(__TBL_CRM_HN__,"truename","id=".$admid,'name');
		$admname = ($row)?$row['truename']:'';
		$SQL .= ",admid=".$admid.",admtime=".ADDTIME.",admname='$admname'";
	}
	//
	if($hnid){
		$row = $db->ROW(__TBL_CRM_HN__,"truename","id=".$hnid,'name');
		$hnname = ($row)?$row['truename']:'';
		$SQL .= ",hnid=".$hnid.",hntime=".ADDTIME.",hnname='$hnname'";
	}else{
		$SQL .= ",hnid=0,hntime=0,hnname=''";
	}
	//
	if($hnid2){
		$row = $db->ROW(__TBL_CRM_HN__,"truename","id=".$hnid2,'name');
		$hnname2 = ($row)?$row['truename']:'';
		$SQL .= ",hnid2=".$hnid2.",hntime2=".ADDTIME.",hnname2='$hnname2'";
	}else{
		$SQL .= ",hnid2=0,hntime2=0,hnname2=''";
	}
	$tmeplist = explode('_',$ulist);
	if(count($tmeplist)>=1){
		foreach($tmeplist as $uid){
			if ( !ifint($uid) )textmsg('uid：'.$uid.'不存在');
			$sqll="";
			$row = $db->ROW(__TBL_USER__,"nickname,agentid,agenttitle,hnid,hnname,hnid2,hnname2","id=".$uid,'name');
			if ($row){
				$nickname  = trimhtml(dataIO($row['nickname'],'out'));
				$agentid_old    = $row['agentid'];
				$agenttitle_old = $row['agenttitle'];
				$admid_old    = $row['admid'];
				$admname_old  = $row['admname'];
				$hnid_old     = $row['hnid'];
				$hnname_old   = $row['hnname'];
				$hnid_old2    = $row['hnid2'];
				$hnname_old2  = $row['hnname2'];
				$db->query("UPDATE ".__TBL_USER__." SET ".$SQL." WHERE id=".$uid);
				$logstr  = '【CRM】【客户（昵称：'.$nickname.'　UID：'.$uid.'）认领归属修改】<br>';
				if($agentid)$logstr .= '老门店：【'.$agenttitle_old.'（id:'.$agentid_old.'）】->新门店：【'.$agenttitle.'（id:'.$agentid.'）】<br>';
				if($admid)$logstr   .= '老认领：【'.$admname_old.'（id:'.$admid_old.'）】->新认领：【'.$admname.'（id:'.$admid.'）】<br>';
				if($hnid)$logstr    .= '老售前：【'.$hnname_old.'（id:'.$hnid_old.'）】->新售前：【'.$hnname.'（id:'.$hnid.'）】<br>';
				if($hnid2)$logstr   .= '老售后：【'.$hnname_old2.'（id:'.$hnid_old2.'）】->新售后：【'.$hnname2.'（id:'.$hnid2.'）】';
				AddLog($logstr);
				sea_mod_time($uid);
			}			
		}
		$sussess = '分配成功!';
	}
}else{
	$sussess = '';
}
$ulist_str = str_replace("_","　",$ulist);
?>
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title></title>
<script src="<?php echo RES;?>www_zeai_cn.js?<?php echo $_ZEAI['cache_str'];?>"></script>
<?php if(MP=='m'){?>
	<?php echo HEADMETA; ?>
    <link href="<?php echo RES;?>m/css/m.css?<?php echo $_ZEAI['cache_str'];?>" rel="stylesheet" type="text/css" />
    <link href="css/m_adm.css?<?php echo $_ZEAI['cache_str'];?>" rel="stylesheet" type="text/css" />
    <?php require_once 'm_style.php';?>
<?php }else{ ?>
    <link href="css/main.css?<?php echo $_ZEAI['cache_str'];?>" rel="stylesheet" type="text/css">
<?php }?>
<style>.tipspro{line-height:40px;background-color:#fff9f9;color:#f00;font-size:12px}</style>
</head>
<?php if (empty($sussess)){?>
<style>
.table0{width:90%;margin:20px auto}
.table0 td{font-size:14px;padding:10px 5px}
.table0 td:hover{background:none}
.table0 .tdL{width:90px;font-size:14px;color:#666;background:none}
.table0 .tdR {text-align:left;}
</style>
<?php }else{?>
<style>
.sussesstips{width:80%;margin:0 auto;padding-top:100px;font-size:18px;text-align:center}
</style>
<?php }?>
<body class="body">
<?php if (!empty($sussess)){?>
    <script>
	setTimeout(function(){
		parent.location.reload(true);
	},3000);
    </script>
	<div class="sussesstips"><img src="images/sussess.png"><br><br><?php echo $sussess;?><br><br>
    <a class="btn size3 zeaibg" href="javascript:parent.location.reload(true);">关闭</a></div>
<?php exit;}?>
<script>
function chkform(){
	if(!zeai.ifint(agentid.value) || !zeai.ifint(admid.value)){
		zeai.msg('请选择【所属门店】或【认领红娘】');
		return false;
	}else{
		www_zeai_cn_FORM.submit();
	}
	//parent.zeai.confirm('<b class="S16">确定要分配此红娘么？</b>',function(){www_zeai_cn_FORM.submit();})
}
</script>

<?php
if(CRM_ADM){
	if($crm_agentid=='无'){
		$agentid=0;
	}else{
		$crm_agentid = intval($crm_agentid);
		$agentid     = ($crm_agentid>0)?$crm_agentid:$agentid;
	}
	if(!ifint($agentid)){echo '<div class="tipspro">请先选择【所属门店】，再选择对应门店的红娘</div>' ;}
}else{
	$agentid = $session_agentid;
}
?>
<form id="www_zeai_cn_FORM" method="post" action="<?php echo SELF; ?>">
<table class="table0">
    <tr>
    <td class="tdL">待分配客户</td>
    <td class="tdR"><?php echo $ulist_str; ?></td>
    </tr>
    <tr>
        <td class="tdL"><font class="Cf00">* </font>所属门店</td>
        <td class="tdR">
            <?php
			if(CRM_ADM){
				$rt2=$db->query("SELECT id,title FROM ".__TBL_CRM_AGENT__." WHERE flag=1 ORDER BY px DESC,id DESC");
				$total2 = $db->num_rows($rt2);?>
				<select name="agentid" id="agentid" class="W90_ size2 select"  onChange="zeai.openurl('<?php echo SELF;?>?crm_agentid='+this.value+'&ulist=<?php echo $ulist;?>')">
				<option value="无">请选择门店</option>
				<?php
				for($j=0;$j<$total2;$j++) {
					$rows2 = $db->fetch_array($rt2,'num');
					if(!$rows2) break;
					$clss=($agentid==$rows2[0])?' selected':'';?>
					<option value="<?php echo $rows2[0];?>"<?php echo $clss;?>><?php echo dataIO($rows2[1],'out');?></option>
					<?php
				}
			}else{
				echo $session_agenttitle;
				echo '<input name="agentid" id="agentid" type="hidden" value="'.$agentid.'">';
			}
            ?>
			</select>
        </td>
    </tr>
	<?php if(ifint($agentid))$SQL="AND agentid=$agentid";?>
    <tr>
        <td class="tdL"><font class="Cf00">* </font>认领红娘</td>
        <td class="tdR">
			<select name="admid" id="admid" class="W90_ size2 select">
			<option value="">请选择录入/认领人</option>
            <?php
			if(ifint($agentid)){
			$rt2=$db->query("SELECT id,username,truename FROM ".__TBL_CRM_HN__." WHERE flag=1 ".$SQL." ORDER BY px DESC,id DESC");
			$total2 = $db->num_rows($rt2);
			if($total2>0){
			for($j=0;$j<$total2;$j++) {
				$rows2 = $db->fetch_array($rt2,'num');
				if(!$rows2) break;
				$clss=($admid==$rows2[0])?' selected':'';?>
				<option value="<?php echo $rows2[0];?>"<?php echo $clss;?>><?php echo dataIO($rows2[2],'out').'　｜　'.dataIO($rows2[1],'out').'　｜　ID:'.dataIO($rows2[0],'out');?></option><?php
			}}}?>
			</select>
        </td>
    </tr>
    
    <tr>
        <td class="tdL">售前红娘</td>
        <td class="tdR">
			<select name="hnid" class="W90_ size2 select">
			<option value="">请选择售前</option>
            <?php
			if(ifint($agentid)){
			$rt2=$db->query("SELECT id,username,truename  FROM ".__TBL_CRM_HN__." WHERE flag=1 AND FIND_IN_SET('sq',crmkind) ".$SQL." ORDER BY px DESC,id DESC");
			$total2 = $db->num_rows($rt2);
			if($total2>0){
			for($j=0;$j<$total2;$j++) {
				$rows2 = $db->fetch_array($rt2,'num');
				if(!$rows2) break;
				$clss=($hnid==$rows2[0])?' selected':'';?>
				<option value="<?php echo $rows2[0];?>"<?php echo $clss;?>><?php echo dataIO($rows2[2],'out').'　｜　'.dataIO($rows2[1],'out').'　｜　ID:'.dataIO($rows2[0],'out');?></option><?php
			}}}?>
			</select>
        </td>
    </tr>
    
    <tr>
        <td class="tdL">售后红娘</td>
        <td class="tdR">
			<select name="hnid2" class="W90_ size2 select">
			<option value="">请选择售后</option>
            <?php
			if(ifint($agentid)){
			$rt2=$db->query("SELECT id,username,truename  FROM ".__TBL_CRM_HN__." WHERE flag=1 AND FIND_IN_SET('sh',crmkind) ".$SQL." ORDER BY px DESC,id DESC");
			$total2 = $db->num_rows($rt2);
			if($total2>0){
			for($j=0;$j<$total2;$j++) {
				$rows2 = $db->fetch_array($rt2,'num');
				if(!$rows2) break;
				$clss=($hnid2==$rows2[0])?' selected':'';?>
				<option value="<?php echo $rows2[0];?>"<?php echo $clss;?>><?php echo dataIO($rows2[2],'out').'　｜　'.dataIO($rows2[1],'out').'　｜　ID:'.dataIO($rows2[0],'out');?></option><?php
			}}}?>
			</select>
        </td>
    </tr>

</table>
<br><br><br><br><br><br><div class="savebtnbox"><button type="button" class="btn size3 zeaibg" onclick="chkform()">确定分配</button></div>
<input type="hidden" name="submitok" value="sendupdate">
<input type="hidden" name="ulist" value="<?php echo $ulist; ?>">
</form>
</body>
</html>
<?php ob_end_flush();?>